package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Card;
import com.pojo.Staff;
import com.pojo.StaffHistory;
import com.service.CardService;
import com.service.StaffHistoryService;
import com.service.StaffService;
import com.util.ValidateHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/staffHis")
public class StaffHistoryAction {

    @Autowired
    private StaffService staffService;

    @Autowired
    private StaffHistoryService staffHistoryService;

    @Autowired
    private CardService cardService;

    /**
     *查询员工（已审核）
     * @param name  姓名
     * @param page  页数
     * @param limit 每页条数
     * @return
     */
    @RequestMapping("/selectStaffHis")
    public ResponseBean selectStaffHis(String name, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit ,
                                       @RequestParam(required = false) String wuye , @RequestParam(required = false) String qiye, HttpServletRequest request){
//        System.out.println("员工姓名："+name+" 页数："+page+" 每页条数："+limit+" 物业："+wuye+" 企业："+qiye);
//        BusinessOccupation businessOccupation = (BusinessOccupation) request.getSession().getAttribute("businessOccupation");
//        GroupOffice groupOffice = (GroupOffice) request.getSession().getAttribute("groupOffice");
        String queryStr = null;
//        if (null != businessOccupation) {
            queryStr = "gs";
//        }else if (null != groupOffice) {
//            queryStr = "jt";
//        }
        IPage iPage = staffHistoryService.selectStaffHis(name,page,limit , wuye , qiye, queryStr) ;
        return new ResponseBean(0,iPage.getTotal(),"成功", iPage.getRecords());
    }

    /**
     * 删除员工
     * @param id    员工ID
     * @return
     */
    @RequestMapping("/deleteStaffHis")
    public ResponseBean deleteStaffHis(String id) {
        if (staffHistoryService.removeById(id)) {
            return ResponseBean.ok("删除成功");
        }
        return ResponseBean.error("删除失败");
    }

    /**
     * 恢复员工
     * @param id    员工ID
     * @return
     */
    @RequestMapping("/hfStaff")
    public ResponseBean hfStaff(String id) {
        StaffHistory staffHis = staffHistoryService.getById(id);
        boolean existFlag = false;
        Staff staffDb = staffService.getById(id);
        Staff staff = new Staff();
        // 判断恢复员工的ID在staff表是否存在，存在则重新生成ID，不存在则用原来老的ID
        if (staffDb == null) {
            BeanUtils.copyProperties(staffHis, staff);
            existFlag = true;
        }else {
            BeanUtils.copyProperties(staffHis, staff, "id");
            existFlag = false;
        }

        if (staffService.save(staff)) {
            if (staffHistoryService.removeById(id)) {
                if (!existFlag) {
                    // 判断恢复员工是否有卡号，有卡号则将卡表绑定的staff_id 重新绑定为新的
                    if (ValidateHelper.isNotEmptyString(staff.getCardNo())) {
                        Card card = cardService.getCardByCardNo(staff.getCardNo());
                        card.setStaffId(staff.getId());
                        cardService.updateById(card);
                    }
                }
                return ResponseBean.ok("恢复成功");
            }
        }
        return ResponseBean.error("恢复失败");
    }
}
